﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Web.Services.Protocols;

namespace SPWebShell.cmdlet
{
    /// <summary>
    /// Removes the user from the site collection
    /// </summary>
    [Cmdlet(VerbsCommon.Remove, "SPUser")]
    public class RemoveUserFromSite : CmdLetBase
    {

        public RemoveUserFromSite()
        {

        }
        
        [Parameter(Mandatory = true, HelpMessage="The absolute URL to the SharePoint site collection. e.g. http://server/sites/SiteA/")]
        [ValidateNotNullOrEmpty]
        public string Url { get; set; }

        [Parameter(Mandatory = true, HelpMessage = "The login of the user who will be added to the specified group")]
        [ValidateNotNullOrEmpty]
        [Alias("name", "user","username")]
        public string Login { get; set; }

        protected override void ProcessRecord()
        {
            try
            {
                VerboseLogger("Begin cmdlet RemoveUserFromSite");
                VerboseLogger("Start time: " + DateTime.Now.ToString());
                ISPRepository repos = base.SPRepos;
                repos.RemoveUserFromSite(this.Url, this.Login);
                VerboseLogger("End time: " + DateTime.Now.ToString());
                base.ProcessRecord();
            }
            catch (SoapException ex)
            {
                string message = ex.Detail.OuterXml;
                throw new Exception(message, ex);
            }
            finally
            {
                VerboseLogger("Exiting cmdlet AddSPGroupUser");
                WriteVerbose("End time: " + DateTime.Now.ToString());
            }
        }
    }
}
;